package com.vinaprime.primeschedule.databaselayer;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
@SuppressLint("NewApi")
public class DatabaseController_DL extends SQLiteOpenHelper{
	private static final String DATABASE_NAME = "DB_TaskList.db";
	//private static final String DATABASE_NAME = "contract.db";
	private static final int DATABASE_VERSION = 1;
	
	private Context myContext;
	private String myPath;
	
	public DatabaseController_DL(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.myContext = context;
	}
	
	public DatabaseController_DL(Context context, String path){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.myContext = context;
		this.myPath = path;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub
		//this.getReadableDatabase();
		try {
			if (this.checkDatabaseExist(this.myPath))
			{
				//do nothing - database already exist
			}
			else
			{
				//	this.copyDatabase(this.myPath);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
	}

	
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	
	public Context getMyContext() {
		return myContext;
	}

	public void setMyContext(Context myContext) {
		this.myContext = myContext;
	}

	public String getMyPath() {
		return myPath;
	}

	public void setMyPath(String myPath) {
		this.myPath = myPath;
	}
	
	public String getDatabaseName()
	{
		return DatabaseController_DL.DATABASE_NAME;
	}
	
	public int getDatabaseVersion()
	{
		return DatabaseController_DL.DATABASE_VERSION;
	}
	
	public boolean checkDatabaseExist(String path) throws IOException{
		SQLiteDatabase chkDBExist = null;
		boolean result = false;
		
		try
		{
			chkDBExist = SQLiteDatabase.openDatabase(path + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
		}
		catch(SQLiteException ex)
		{
			//database doesn't exist yet.
		}
		
		if (chkDBExist != null)
		{
			chkDBExist.close();
		}
		
		return chkDBExist != null ? true : false;
		//return result;
	}
	
	public void copyDatabase(String path) throws IOException{
		this.getReadableDatabase();
		this.getWritableDatabase();
		//Open your local db as the input stream
		InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
		
		//Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(path + DATABASE_NAME);
		
		//transfer bytes from the inputfile to the outputfile
	    byte[] buffer = new byte[1024];
	    int length;
	    while ((length = myInput.read(buffer))>0){
	        myOutput.write(buffer, 0, length);
	    }

	    //Close the streams
	    myOutput.flush();
	    myOutput.close();
	    myInput.close();
	}
}
